﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using LuKou.Web.BLL;
using LuKou.Web.BLL.Extensions;

namespace LuKou.Web.Helper
{
    public static class PagerHelper
    {
        private const int defaultDisplayCount = 11;
        /// <summary>
        /// Pager Helper Extensions
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="currentPage">当前页码</param>
        /// <param name="pageSize">页面显示的数据条目</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="toDisplayCount">Helper要显示的页数</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount, int toDisplayCount)
        {
            RenderPager pager = new RenderPager(htmlHelper, currentPage, pageSize, totalCount, toDisplayCount);
            return pager.RenderHtml();
        }
        /// <summary>
        /// Pager Helper Extensions
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="currentPage">当前页码</param>
        /// <param name="pageSize">页面显示的数据条目</param>
        /// <param name="totalCount">总记录数</param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
        {
            RenderPager pager = new RenderPager(htmlHelper, currentPage, pageSize, totalCount, defaultDisplayCount);
            return pager.RenderHtml();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="controllerName"></param>
        /// <param name="actionName"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static string Pager(this HtmlHelper htmlHelper, string controllerName, string actionName, int currentPage, int pageSize, int totalCount)
        {
            RenderPager pager = new RenderPager(htmlHelper, controllerName, actionName, currentPage, pageSize, totalCount, defaultDisplayCount);
            return pager.RenderHtml();
        }
        /// <summary>
        /// IEnumerable extensions
        /// </summary>
        public static PagedList<T> ToPageList<T>(this IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
        {
            return new PagedList<T>(source, pageIndex, pageSize, totalCount);
        }
    }

    public class RenderPager
    {
        #region 字段
        /// <summary>
        /// 当前页面的ViewContext
        /// </summary>
        private string controllerName;
        /// <summary>
        /// 
        /// </summary>
        private string actionName;
        /// <summary>
        /// 当前页码
        /// </summary>
        private readonly int currentPage;
        /// <summary>
        /// 页面要显示的数据条数
        /// </summary>
        private readonly int pageSize;
        /// <summary>
        /// 总的记录数
        /// </summary>
        private readonly int totalCount;
        /// <summary>
        /// Pager Helper 要显示的页数
        /// </summary>
        private readonly int toDisplayCount;
        private readonly HtmlHelper html;
        RouteValueDictionary routeValues;
        #endregion

        #region 构造函数

        public RenderPager(HtmlHelper html, int currentPage, int pageSize, int totalCount, int toDisplayCount)
            : this(html, null, null, currentPage, pageSize, totalCount, toDisplayCount)
        { }

        public RenderPager(HtmlHelper html, string actionName, string controllerName, int currentPage, int pageSize, int totalCount, int toDisplayCount)
        {
            this.html = html;
            this.actionName = actionName;
            this.controllerName = controllerName;
            this.currentPage = currentPage;
            this.pageSize = pageSize;
            this.totalCount = totalCount;
            this.toDisplayCount = toDisplayCount;

            InitRouteValues();
        }

        private void InitRouteValues()
        {
            routeValues = new RouteValueDictionary();
            NameValueCollection strQuery = this.html.ViewContext.RequestContext.HttpContext.Request.QueryString;
            for (int i = 0; i < strQuery.Count; i++)
            {
                string value = strQuery[i];
                string key = strQuery.GetKey(i);
                if (string.IsNullOrEmpty(value)
                    || string.IsNullOrEmpty(key))
                {
                    continue;
                }
                routeValues.Add(key, value);
            }
            object pageIndex;
            if (!routeValues.TryGetValue("pageindex", out pageIndex))
            {
                pageIndex = 1;
                routeValues.Add("pageindex", pageIndex);
            }
        }

        #endregion

        #region 方法
        public string RenderHtml()
        {
            if (totalCount <= pageSize)
                return string.Empty;
            //总页数
            int pageCount = (int)Math.Ceiling(this.totalCount / (double)this.pageSize);
            //起始页
            int start = 1;
            //结束页
            int end = toDisplayCount;
            if (pageCount < toDisplayCount) end = pageCount;
            //中间值
            int centerNumber = toDisplayCount / 2;
            if (pageCount > toDisplayCount)
            {
                //显示的第一位
                int topNumber = currentPage - centerNumber;
                if (topNumber > 1)
                {
                    start = topNumber;
                }
                if (topNumber > pageCount - toDisplayCount)
                {
                    start = pageCount - toDisplayCount + 1;
                }
                //显示的最后一位
                int endNumber = currentPage + centerNumber;

                if (currentPage >= pageCount - centerNumber)
                {
                    end = pageCount;
                }
                else
                {
                    if (endNumber > toDisplayCount)
                    {
                        end = endNumber;
                    }
                }
            }

            StringBuilder sb = new StringBuilder();
            //Previous
            if (this.currentPage > 1)
            {
                routeValues["pageindex"] = 1;
                sb.Append(GeneratePageLink(" << "));

                routeValues["pageindex"] = currentPage - 1;
                sb.Append(GeneratePageLink("Prev"));
            }

            if (start > 1)
            {
                sb.Append(GeneratePageLink("1"));
                sb.Append("...");
            }

            //end Previous
            for (int i = start; i <= end; i++)
            {
                routeValues["pageindex"] = i;
                if (i == this.currentPage)
                {
                    sb.AppendFormat("<span class=\"current\">{0}</span>", i);
                }
                else
                {
                    sb.Append(GeneratePageLink(i.ToString()));
                }
            }

            //Next
            if (end < pageCount)
            {
                sb.Append("...");
                routeValues["pageindex"] = pageCount;
                sb.Append(GeneratePageLink(pageCount.ToString()));
            }

            if (this.currentPage < pageCount)
            {
                routeValues["pageindex"] = currentPage + 1;
                sb.Append(GeneratePageLink("Next"));

                routeValues["pageindex"] = pageCount;
                sb.Append(GeneratePageLink(">>"));                  
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成Page的链接
        /// </summary>
        /// <param name="linkText">文字</param>
        /// <param name="PageNumber">页码</param>
        /// <returns></returns>
        private string GeneratePageLink(string linkText)
        {
            if (!string.IsNullOrEmpty(actionName))
            {
                routeValues["action"] = actionName;
            }
            if (!string.IsNullOrEmpty(controllerName))
            {
                routeValues["controller"] = controllerName;
            }
            return HtmlHelper.GenerateRouteLink(html.ViewContext.RequestContext, html.RouteCollection, linkText, null, routeValues, null);
        }
        #endregion
    }

    
}

